import sys

# print sys.argv[0]
# print sys.argv[1]
# print sys.argv[2]

def main(log_name, action_str, host_id, msg_id, log_processed_name):
    # print log_name
    # print host_id
    # print log_processed_name
    log_start=False
    real_log_start=False
    write_end=False
    log = open(log_name)
    log_processed = open(log_processed_name, "w+")
    lines = log.readlines()
    for line in lines:
        # line = line.strip()
        if not len(line):
            continue
        if line.startswith("(c)"):
            log_start = True
            continue
        if line.startswith("(done)"):
            log_start = False
            real_log_start = False
            continue
        if action_str in line:
            if host_id in line:
                real_log_start = True
                log_processed.write(line)
            else :
                real_log_start = False
            continue
        if line.startswith("----msg"):
            if real_log_start == True:
                if msg_id in line:
                    log_processed.write(line)
    log.close()
    log_processed.close()


main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])